package cn.itsource.pethome.system.pet.service.impl;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.jwt.LoginContextUtils;
import cn.itsource.pethome.system.pet.domain.SearchMasterMsg;
import cn.itsource.pethome.system.pet.domain.SearchMasterMsgAuditLog;
import cn.itsource.pethome.system.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.itsource.pethome.system.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pethome.system.pet.service.ISearchMasterMsgService;
import cn.itsource.pethome.base.service.impl.IBaseServiceImpl;
import cn.itsource.pethome.system.shop.domain.Shop;
import cn.itsource.pethome.system.shop.mapper.ShopMapper;
import cn.itsource.pethome.system.user.domain.User;
import cn.itsource.pethome.utils.AuthService;
import cn.itsource.pethome.utils.BaiduMapUtils;
import cn.itsource.pethome.utils.BusinessException;
import cn.itsource.pethome.utils.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xlh
 * @since 2023-02-08
 */
@Service
public class SearchMasterMsgServiceImpl extends IBaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private LoginContextUtils loginContextUtils;
    @Autowired
    private AuthService authService;
    @Autowired
    private SearchMasterMsgAuditLogMapper logMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //先审核
        if(!authService.verification(searchMasterMsg.getName())){
            throw new BusinessException("你输入的内容，存在敏感词汇!");
        }
        SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
        searchMasterMsgAuditLog.setAuditTime(new Date());
        searchMasterMsgAuditLog.setNote("审核通过");
        searchMasterMsgAuditLog.setState(BaseConstants.Publish.STATE_PASS);

        User loginUser = (User)loginContextUtils.getLoginUser(request);
        //用户信息
        searchMasterMsg.setUserId(loginUser.getId());
        //状态
        searchMasterMsg.setState(BaseConstants.Publish.STATE_PASS);
        //审核的备注  先不管

        //店铺       计算是否在50公里之类  在这个返回内就推送店铺 不在就放入寻主池
        Point point = BaiduMapUtils.getPoint(searchMasterMsg.getAddress());
        List<Shop> collect = shopMapper.findAll().stream()
                .filter(shop -> {   //过滤在这个范围内的
                    Point point1 = BaiduMapUtils.getPoint(shop.getAddress());
                    return BaiduMapUtils.getDistance(point, point1) < 50000;
                }).collect(Collectors.toList());

        if(!collect.isEmpty()){ //如果是空的就是true
            Double min = 50000.0;
            Long shopId = null;
            for (Shop shop : collect) {
                double distance = BaiduMapUtils.getDistance(point, BaiduMapUtils.getPoint(shop.getAddress()));
                if(distance<min){
                    min = distance;
                    shopId = shop.getId();
                }
            }
            searchMasterMsg.setShopId(shopId);
        }


        searchMasterMsgMapper.add(searchMasterMsg);
        //设置主表id  必须放在主表的添加后面
        searchMasterMsgAuditLog.setMsgId(searchMasterMsg.getId());
        logMapper.add(searchMasterMsgAuditLog);
    }
}
